﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks.Dataflow;

namespace NewSolution
{
    public class IntegrateBlock
    {
        private readonly object _lock = new object();
        private List<byte> accumulatedFrames = new List<byte>();
        private int frameNumber = 1;

        public IEnumerable<(byte[], int)> Integration(byte[] frame, int flag)
        {
            lock (_lock)
            {
                accumulatedFrames.AddRange(frame);

                if (flag == 1)
                {
                    byte[] integratedFrame = accumulatedFrames.ToArray();
                    accumulatedFrames.Clear();
                    yield return (integratedFrame, frameNumber++);
                }
            }
        }

        public TransformBlock<(byte[], int), (byte[], int)> CreateTransformBlock()
        {
            return new TransformBlock<(byte[], int), (byte[], int)>(
                frame =>
                {
                    var integratedFrames = Integration(frame.Item1, frame.Item2).ToList();
                    return integratedFrames.First();
                });
        }
    }
}